from django.core.context_processors import csrf
from django.http import HttpResponse
import json
from bazy.models import User
from django.http import HttpResponse
import hashlib
from bazy.login import Login

def getCrsfToken(request):
	return HttpResponse(json.dumps({'csrf_token':csrf(request)['csrf_token']+""}))

def login (request):
	log = Login(request);
	if log.isLogged():
		return HttpResponse(json.dumps({'Error':0}))
	if request.method=='POST':
		username = request.POST.get('username',-1)
		password = request.POST.get('password',-1)
		if log.login(username, password):
			return HttpResponse(json.dumps({'Error':0}))
		else:
			return HttpResponse(json.dumps({'Error':-1}))
	else:   
		return HttpResponse(json.dumps({'Error':-2})) 

			

def logout(request):
	log = Login(request);
	log.logout()
	return HttpResponse(json.dumps({'Error':0}))


